package by.exposit.service;

import by.exposit.dao.DonateDAO;
import by.exposit.dao.entity.DonateEntity;
import by.exposit.domain.Donate;
import by.exposit.domain.User;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DonateServiceImpl implements DonateService {

    @Autowired
	private DonateDAO donateDAO;

	@Override	
	public void saveDonate(Donate donate) {
        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        donateDAO.save(mapper.map(donate, DonateEntity.class));
	}

	@Override
	public List<Donate> donateList() {

	    Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Donate> donates = new ArrayList<Donate>();
        List<DonateEntity> donateEntities = donateDAO.getAll();
               for(DonateEntity donate:donateEntities)
               {
                   donates.add(mapper.map(donate,Donate.class));
               }
        return donates;
	}

	@Override
        public List<Donate> donateUserList(User user) {

        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        List<Donate> donates = new ArrayList<Donate>();
        List<DonateEntity> donateEntities = donateDAO.donateUserList(user);
        for(DonateEntity donate:donateEntities)
        {
            donates.add(mapper.map(donate,Donate.class));
        }
        return donates;
	}
}
